package io.finer.erp.jeecg.stock.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.stock.entity.StkIoMaterialLog;
import io.finer.erp.jeecg.stock.mapper.StkIoMaterialLogMapper;
import io.finer.erp.jeecg.stock.service.IStkIoMaterialLogService;

/**
 * @Description: 物料出入库记录表
 * @Author: jeecg-boot
 * @Date:   2020-09-24
 * @Version: V1.0
 */
@Service
public class StkIoMaterialLogServiceImpl extends ServiceImpl<StkIoMaterialLogMapper, StkIoMaterialLog> implements IStkIoMaterialLogService {
	/**
	 * @Description 添加出入库记录 方便以后核算成本
	 * @param billId 出入库单ID
	 * @param createTime 创建时间
	 * @param entityId 出入库单明细ID
	 * @param ioType 出入库类型
	 * @param isAccount 是否已经核算成本
	 * @param material 物料
	 * @param unitId 单位ID
	 * @param orderId 生产订单ID
	 * @param inOrOut 是出库还是入库
	 * @param cost 成本
	 * @param amount 出入库数量
	 * Create at: 2020-09-24 13:42:01
	 * @author: duanyong
	 * Revision:
	 *    2020-09-24 13:42:01 - first revision by duanyong
	 *    2020-09-25 17:35:11 - update by duanyong    
	 */
	public void insertLog(String billId, Date createTime
			, String entityId, String ioType
			, Integer isAccount, BasMaterial material, String unitId
			, String orderId, Integer inOrOut, BigDecimal cost
			, BigDecimal amount, String processId) {
		StkIoMaterialLog log = StkIoMaterialLog.builder()
				.billId(billId)
				.createTime(createTime)
				.entityId(entityId)
				.ioType(ioType)
				.isAccount(0)
				.materialId(material.getId())
				.unitId(unitId)
				.productOrderId(orderId)
				.inOrOut(inOrOut)
				.width(material.getWidth())
				.length(material.getLength())
				.square(material.getSquare())
				.amount(amount)
				.qty(amount)
				.procedureId(processId)
				.materialCode(material.getCode())
				.build();
		if (null != cost && BigDecimal.ZERO.compareTo(cost) < 0) {
			log.setCost(cost);
		}
		save(log);
	}

	@Override
	public List<StkIoMaterialLog> getByProcess(String processId) {
		LambdaQueryWrapper<StkIoMaterialLog> query = new LambdaQueryWrapper<>();
		query.eq(StkIoMaterialLog::getProcedureId, processId);
		query.orderByDesc(StkIoMaterialLog::getCreateTime);
		List<StkIoMaterialLog> list = list(query);
		return list;
	}

	/**
	 * @Description: 根据原单ID和物料ID查询工序ID
	 * @param orderNum
	 * @param id
	 * @return
	 * Create at: 2020-10-30 14:21:43
	 * @author: duany
	 * Revision:
	 *    2020-10-30 14:21:43 - first revision by duany
	 *        
	 */
	@Override
	public String getProcessIdFromLog(String orderNum, String id) {
		LambdaQueryWrapper<StkIoMaterialLog> query = new LambdaQueryWrapper<>();
		query.eq(StkIoMaterialLog::getMaterialId, id);
		query.eq(StkIoMaterialLog::getProductOrderId, orderNum);
		query.orderByDesc(StkIoMaterialLog::getCreateTime);
		List<StkIoMaterialLog> list = list(query);
		if(null != list && 0 != list.size()) {
			StkIoMaterialLog log = list.get(0);
			return log.getProcedureId();
		}
		return null;
	}

	@Override
	public List<StkIoMaterialLog> selectByEntryIds(List<String> entryIds) {
		LambdaQueryWrapper<StkIoMaterialLog> query = new LambdaQueryWrapper<>();
		query.in(StkIoMaterialLog::getEntityId, entryIds);
		return list(query);
	}

	@Override
	public List<StkIoMaterialLog> getByMaterial(String materialId) {
		LambdaQueryWrapper<StkIoMaterialLog> query = new LambdaQueryWrapper<>();
		query.eq(StkIoMaterialLog::getMaterialId, materialId);
		query.orderByDesc(StkIoMaterialLog::getCreateTime);
		return list(query);
	}
}
